
Set::Set(){
	
}

Set::~Set(){
	
}

void Set::insert( int e ){
	elements[e] = true;
}

void Set::print(){
	map<int,bool>::iterator it = elements.begin();
	cout << "{ ";
	while( it != elements.end() ){
		cout << it->first << " ";
		++it;
	}
	cout << "}" << endl;
}

void Set::intersection( Set *s ){
	map<int,bool> intersection;
	map<int,bool>::iterator it = elements.begin();
	while( it != elements.end() ){
		if( s->elements.find(it->first) != s->elements.end() )
			intersection[it->first] = true;
		++it;
	}
	elements = intersection;
}

Set* Set::intersection( Set *s1, Set* s2 ){
	Set* intersection = new Set();
	map<int,bool>::iterator it = s1->elements.begin();
	while( it != s1->elements.end() ){
		if( s2->elements.find(it->first) != s2->elements.end() )
			intersection->insert(it->first);
		++it;
	}
	return intersection;
}

void Set::union_( Set *s ){
	map<int,bool>::iterator it = s->elements.begin();
	while( it != s->elements.end() ){
		elements[it->first] = true;
		++it;
	}
}

Set* Set::union_( Set* s1, Set* s2 ){
	Set* union_ = new Set();
	union_->elements = s1->elements;
	map<int,bool>::iterator it = s2->elements.begin();
	while( it != s2->elements.end() ){
		union_->elements[it->first] = true;
		++it;
	}
	return union_;
}

bool Set::equals(Set* s){
	if( cardinality() != s->cardinality() )
		return false;
	
	map<int,bool>::iterator it = elements.begin();
	while( it != elements.end() ){
		if( !s->elements[it->first] )
			return false;
		++it;
	}
	return true;
}

bool Set::equals(Set* s1,Set* s2){
	if( s1->cardinality() != s2->cardinality() )
		return false;
	
	map<int,bool>::iterator it = s1->elements.begin();
	while( it != s1->elements.end() ){
		if( !s2->elements[it->first] )
			return false;
		++it;
	}
	return true;
}

int Set::cardinality(){
	return elements.size();
}

void Set::clear(){
	elements.clear();
}

int* Set::getArray(){
	int *array = new int[elements.size()];
	
	map<int,bool>::iterator it = elements.begin();
	int i = 0;
	while(it != elements.end()){
		array[i] = it->first;
		++it;
		++i;
	}
	
	return array;
}

bool Set::has(int x){
	return elements.find(x) != elements.end();
}